/*
 * Copyright (c) 2022, Egahp
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#ifndef __CHERRY_RB_H__
#define __CHERRY_RB_H__

#ifdef __cplusplus
extern "C" {
#endif

#include <stdbool.h>
#include <stdint.h>

typedef struct {
    uint32_t in;   /*!< Define the write pointer.               */
    uint32_t out;  /*!< Define the read pointer.                */
    uint32_t mask; /*!< Define the write and read pointer mask. */
    void *pool;    /*!< Define the memory pointer.              */
} rb_t;

extern int rb_init(rb_t *rb, void *pool, uint32_t size);
extern void rb_reset(rb_t *rb);
extern void rb_reset_read(rb_t *rb);

extern uint32_t rb_get_size(rb_t *rb);
extern uint32_t rb_get_used(rb_t *rb);
extern uint32_t rb_get_free(rb_t *rb);

extern bool rb_check_full(rb_t *rb);
extern bool rb_check_empty(rb_t *rb);

extern bool rb_write_byte(rb_t *rb, uint8_t byte);
extern bool rb_overwrite_byte(rb_t *rb, uint8_t byte);
extern bool rb_peek_byte(rb_t *rb, uint8_t *byte);
extern bool rb_read_byte(rb_t *rb, uint8_t *byte);
extern bool rb_drop_byte(rb_t *rb);

extern uint32_t rb_write(rb_t *rb, void *data, uint32_t size);
extern uint32_t rb_overwrite(rb_t *rb, void *data, uint32_t size);
extern uint32_t rb_peek(rb_t *rb, void *data, uint32_t size);
extern uint32_t rb_read(rb_t *rb, void *data, uint32_t size);
extern uint32_t rb_drop(rb_t *rb, uint32_t size);

extern void *rb_linear_write_setup(rb_t *rb, uint32_t *size);
extern void *rb_linear_read_setup(rb_t *rb, uint32_t *size);
extern uint32_t rb_linear_write_done(rb_t *rb, uint32_t size);
extern uint32_t rb_linear_read_done(rb_t *rb, uint32_t size);

#ifdef __cplusplus
}
#endif

#endif /* !__CHERRY_RB_H__ */
